package ru.urfc.portal.ui.client.googlets.admin;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.controllers.GridController;
import ru.urfc.portal.ui.client.controllers.events.GetEvent;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.descriptor.GridField;
import ru.urfc.portal.ui.client.model.catalogs.booleans.ClientAccessEnabled;
import ru.urfc.portal.ui.client.model.catalogs.booleans.ClientAdminRegistered;
import ru.urfc.portal.ui.client.widgets.ClientFilter;
import ru.urfc.portal.ui.client.widgets.MegaGrid;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;

public class ClientList extends BaseGooglet {

	private static final int NAME_FILTER_WIDTH = 200;
	private static final int INN_FILTER_WIDTH = 100;
	private static final int OGRN_FILTER_WIDTH = 100;

	interface Binder extends UiBinder<Widget, ClientList> {}
	private static Binder binder = GWT.create(Binder.class);

	@UiField Element title;
	@UiField ClientFilter clientFilter;
	@UiField MegaGrid grid;
	@UiField Button search;

	public Type<GridController> SEARCH = new Type<GridController>();

	@UiHandler("search")
	void search(ClickEvent e) {
		getEventsDispatcher().fireEvent(new GetEvent(SEARCH));
	}
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		title.setInnerText("Клиенты");
		clientFilter.getName().setWidth(NAME_FILTER_WIDTH);
		clientFilter.getInn().setWidth(INN_FILTER_WIDTH);
		clientFilter.getOgrn().setWidth(OGRN_FILTER_WIDTH);
		clientFilter.setupButton(search.getElement());
		clientFilter.init(this);
		
		GridDescriptor descriptor = new GridDescriptor(CLIENT, CLIENTS);
		descriptor.addAnchorColumn("contragent/name", "id", PAGE_CLIENT_EDIT, "Наименование", NAME_FILTER_WIDTH);
		descriptor.addTextColumn("inn", "ИНН", INN_FILTER_WIDTH);
		descriptor.addTextColumn("ogrn", "ОГРН", OGRN_FILTER_WIDTH);
		descriptor.addTextColumn("branch/name", "Филиал", 100);
		GridField autoExpandColumnField = 
			descriptor.addBoolCIColumn("adminRegistered", ClientAdminRegistered.class, "Администратор", 100);
		descriptor.addBoolCIColumn("accessEnabled", ClientAccessEnabled.class, "Доступ", 100);
		
		register(SEARCH, createController(descriptor, grid)).setFilter(clientFilter);
        grid.setAutoExpandColumn(autoExpandColumnField.getName());
	}

}
